package com.example.wb.myfirstandroid;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by wb on 16-10-22.
 */

public class MyAdapter extends ArrayAdapter<String> {
    private int resourceId;

    public MyAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        String s = getItem(position);
        View view;
        ViewHolder viewHolder;
        MyListener myListener = null;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) view.findViewById(R.id.txtMyListTest);
            viewHolder.btnInfo = (Button) view.findViewById(R.id.btnInfo);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.textView.setText(s);
//        viewHolder.btnInfo.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                List<String> data = ListViewTestActivity.getData();
//                String s = data.get(position);
//                Toast.makeText(getContext(), s, Toast.LENGTH_SHORT).show();
//            }
//        });

        myListener = new MyListener(position);
        viewHolder.btnInfo.setOnClickListener(myListener);
        return view;
    }

    class ViewHolder {
        TextView textView;
        Button btnInfo;
    }

    private class MyListener implements View.OnClickListener {
        int mPosition;

        public MyListener(int inPosition) {
            mPosition = inPosition;
        }

        @Override
        public void onClick(View v) {
            List<String> data = ListViewTestActivity.getData();
            String s = data.get(mPosition);
            // TODO Auto-generated method stub
            Toast.makeText(getContext(), s, Toast.LENGTH_SHORT).show();
        }

    }
}
